package com.phospherus.flow.service

import com.phospherus.common.service.impl.BaseServiceImpl
import com.phospherus.flow.entity.Approval
import com.phospherus.flow.repository.ApprovalRepository
import org.springframework.stereotype.Service

/**
 * @author songjinlong
 * @version 1.0
 * @since 2017/12/25
 */
@Service
class ApprovalService : BaseServiceImpl<Approval, Long, ApprovalRepository>() {
    fun saveApproval(map: MutableMap<String, Any>) {
        var formInstId = map["formInstId"] as Long
        var approvalMap = map["approvals"] as Map<String, String>?
        if (approvalMap != null && approvalMap.isNotEmpty()) {
            var id = try {
                approvalMap["approval_id"]?.toLong()
            } catch (e: NumberFormatException) {
                null
            }
            var opinion = approvalMap["approval_opinion"]
            var username = approvalMap["approval_username"]
            var time = approvalMap["approval_time"]
            var divName = approvalMap["approval_div_name"]
            var approval = Approval(
                    id = id,
                    approval_opinion = opinion,
                    approval_username = username,
                    approval_time = time,
                    approval_div_name = divName,
                    formInstId = formInstId
            )
            save(approval)
        }
    }

    fun getApprovalsByFormInstId(formInstId: Long): List<Approval> = repository.findByFormInstId(formInstId)
}